package com.csgsystems.im.survey.action;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.csgsystems.im.struts2.action.BaseActionSupport;
import com.csgsystems.im.survey.Category;
import com.csgsystems.im.survey.Language;
import com.csgsystems.im.survey.Question;
import com.csgsystems.im.survey.QuestionType;
import com.csgsystems.im.survey.Survey;
import com.csgsystems.im.survey.SurveyStatus;
import com.csgsystems.im.survey.SurveyType;
import com.csgsystems.im.survey.persistence.LanguageDao;
import com.csgsystems.im.survey.persistence.QuestionDao;
import com.csgsystems.im.survey.persistence.QuestionTypeDao;
import com.csgsystems.im.survey.persistence.SurveyDao;

public class SurveyAction extends BaseActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6365366628601280105L;

	SurveyDao surveyDao = null;
	QuestionDao questionDao = null;
	QuestionTypeDao questionTypeDao = null;
	LanguageDao languageDao = null;
	
	private List<Survey> results = null;
	
	private Long id = null;
	
	String name = null;
	int categoryId = 0;
	long hierarchyItemId = 847;
	String vxml = null;
	int numberOfQuestions = 0;
	int voiceRecording = 0;
	SurveyStatus status = SurveyStatus.Inactive;
	SurveyType type = SurveyType.Web;
	boolean resultFollowup = false;
	boolean displayResultAverage = false;
	
	private List<Question> questions = Collections.EMPTY_LIST;
	
	public String list() {
		String response = ERROR;
		try {
			results = surveyDao.findByHierarchyItem(847);		// Will be tied into users hierarchyItemId
			getSession().setAttribute("exampleEntities", results);
			response = SUCCESS;
			
		} catch (Exception e) {
			addActionError(e.getMessage());
			
		}

		return response;
	}
	
	public String view() {
		String response = ERROR;
		
		try {
			Survey view = surveyDao.findById(id);
			getSession().setAttribute("entityModel", view);	// Used for UpdateAction.
			
			this.name = view.getName();
			this.vxml = view.getVxml();
			this.numberOfQuestions = view.getNumberOfQuestions();
			this.categoryId = view.getCategoryId();
			this.status = view.getStatus();
			this.type = view.getType();
			this.hierarchyItemId = view.getHierarchyItemId();
			this.resultFollowup = view.isResultFollowup();
			this.displayResultAverage = view.isDisplayResultAverage();
            this.voiceRecording = view.getVoiceRecording();
            this.questions = questionDao.findBySurveyId(view.getId());
			response = SUCCESS;
			
		} catch (Exception e) {
			addActionError(e.getMessage());
			
		}
		return response;
	}
	
	public String add() {
		return SUCCESS;
	}
	
	public String create() {
		String response = ERROR;
		try {
			surveyDao.create(new Survey(name, categoryId, 847, vxml,
					numberOfQuestions, voiceRecording, type, resultFollowup,
					displayResultAverage, SurveyStatus.Inactive));

			response = SUCCESS;
			
		} catch (Exception e ) {
			addActionError(e.getMessage());
			
		}
		return response;
	}
	
	public String update() {
		String response = ERROR;
		try {
			Survey model = (Survey) getSession().getAttribute("entityModel"); // Set via ViewAction
			model.setName(name);
			model.setCategoryId(categoryId);
			model.setHierarchyItemId(hierarchyItemId);
			model.setVxml(vxml);
			model.setNumberOfQuestions(numberOfQuestions);
			model.setVoiceRecording(voiceRecording);
			model.setStatus(status);
			model.setType(type);
			model.setResultFollowup(resultFollowup);
			model.setDisplayResultAverage(displayResultAverage);

			surveyDao.update(model.getId(), model);
			getSession().removeAttribute("entityModel");
			response = SUCCESS;

		} catch (Exception e) {
			addActionError(e.getMessage());	
			e.printStackTrace();
		}
		return response;
		
	}
	
	public String delete() {
		String response = ERROR;
		try {
			Survey model = (Survey) getSession().getAttribute("entityModel"); // Set via ViewAction
			surveyDao.delete(model);
			getSession().removeAttribute("entityModel");
			response = SUCCESS;
			
		} catch (Exception e) {
			addActionError(e.getMessage());	
			e.printStackTrace();
		}

		return response;
	}
		
	public List<Survey> getResults() {
		return results;
	}
	
	public List<Category> getCategories() {
		return Arrays.asList(Category.values());
	}
	
	public List<SurveyType> getSurveyTypes() {
		return Arrays.asList(SurveyType.values());
	}
	
	public List<QuestionType> getQuestionTypes() {
		List<QuestionType> results = Collections.EMPTY_LIST;
		try {
			results = questionTypeDao.findAll();
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return results;
	}
	
	public List<Language> getLanguages() {
		List<Language> results = Collections.EMPTY_LIST;
		try {
			results = languageDao.findAll();
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return results;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}

	public long getHierarchyItemId() {
		return hierarchyItemId;
	}

	public void setHierarchyItemId(long hierarchyItemId) {
		this.hierarchyItemId = hierarchyItemId;
	}

	public String getVxml() {
		return vxml;
	}

	public void setVxml(String vxml) {
		this.vxml = vxml;
	}

	public int getNumberOfQuestions() {
		return numberOfQuestions;
	}

	public void setNumberOfQuestions(int numberOfQuestions) {
		this.numberOfQuestions = numberOfQuestions;
	}

	public int getVoiceRecording() {
		return voiceRecording;
	}

	public void setVoiceRecording(int voiceRecording) {
		this.voiceRecording = voiceRecording;
	}

	public SurveyStatus getStatus() {
		return status;
	}

	public void setStatus(SurveyStatus status) {
		this.status = status;
	}

	public String getType() {
		return type.toString();
	}

	public void setType(String type) {
		this.type = SurveyType.valueOf(type);
	}

	public boolean isResultFollowup() {
		return resultFollowup;
	}

	public void setResultFollowup(boolean resultFollowup) {
		this.resultFollowup = resultFollowup;
	}

	public boolean isDisplayResultAverage() {
		return displayResultAverage;
	}

	public void setDisplayResultAverage(boolean displayResultAverage) {
		this.displayResultAverage = displayResultAverage;
	}

	public List<Question> getQuestions() {
		return questions;
	}

	public SurveyDao getSurveyDao() {
		return surveyDao;
	}

	public QuestionDao getQuestionDao() {
		return questionDao;
	}

	public LanguageDao getLanguageDao() {
		return languageDao;
	}

	public void setSurveyDao(SurveyDao surveyDao) {
		this.surveyDao = surveyDao;
	}

	public void setQuestionDao(QuestionDao questionDao) {
		this.questionDao = questionDao;
	}

	public void setQuestionTypeDao(QuestionTypeDao questionTypeDao) {
		this.questionTypeDao = questionTypeDao;
	}

	public void setLanguageDao(LanguageDao languageDao) {
		this.languageDao = languageDao;
	}
}
